<template>
  <div class="app-container">
    <template>
      <el-row>
        <el-col :span="24">
          <div class="h_top">
            <div class="search_box">
              <el-form :model="query" :inline="true">
                <el-row :gutter="10">
                  <el-col :span="4">
                    <el-form-item>
                      <div>编 号</div>
                      <el-input v-model="query.coding" size="mini" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item>
                      <div>名 称</div>
                      <el-input v-model="query.name" size="mini" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item>
                      <div>状 态</div>
                      <el-select v-model="query.status" size="mini">
                        <el-option v-for="(item, index) in turnStatus" :key="index" :label="item" :value="index" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item>
                      <div>资 产</div>
                      <v-select v-model="query.equipment_id" action="eqList" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item>
                      <div>操 作</div>
                      <el-button type="primary" icon="el-icon-search" size="mini" @click="search">查找</el-button>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
            </div>
            <div class="toolBars">
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-tickets" size="mini" @click="exportHandler">导出</el-button>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-document-add" size="mini" @click="addHandler">增加</el-button>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-delete" size="mini" :disabled="!selection.length" @click="remove">删除</el-button>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-refresh" size="mini" @click="reloadHandler">刷新</el-button>
              </div>
              <div class="fr">总数：<span>{{ total }}</span></div>
            </div>
          </div>
        </el-col>
      </el-row>
    </template>
    <div>
      <el-table v-loading="loading" :data="managementList" size="small" stripe border :header-cell-style="{background:'#eef1f6',color:'#606266'}" style="width: 100%" @selection-change="selectionHandler">
        <el-table-column type="index" label="序号" width="50" align="center" />
        <el-table-column type="selection" align="center" width="45" />
        <el-table-column prop="coding" label="编好" width="150" align="center" />
        <el-table-column prop="name" label="名称" width="150" align="center" />
        <el-table-column prop="eq.name" label="资产" width="100" align="center" />
        <el-table-column prop="date" label="日期" width="150" align="center" />
        <el-table-column prop="eq.manufacturer.name" label="制造厂商" width="150" align="center" />
        <el-table-column prop="eq.department.name" label="使用部门" align="center" />
        <el-table-column prop="status" label="状态" align="center" :formatter="statusFormat" />
        <el-table-column fixed="right" label="操作" width="200" align="center">
          <template scope="scope">
            <el-button type="primary" size="mini" @click="updateHandler(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="danger" @click="confirmDel(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="page" background :page-size="query.limit" layout="total,prev,pager,next,jumper" :total="total" :current-page.sync="query.page" @current-change="index" @size-change="index" />
    </div>
  </div>
</template>
<script>
import API from '@/api/asset/management'
import { turnStatus } from '@/utils/options'
import VSelect from '@/components/select'
import xlsx from 'xlsx'
import exportMethod from '@/mixins/export'
export default {
  inject: ['reload', 'goUrl', 'delOne', 'delMany', 'getStatusFormat'],
  components: {
    VSelect
  },
  mixins: [exportMethod],
  data() {
    return {
      loading: false,
      total: 0,
      query: { page: 1, limit: 15 },
      managementList: [],
      selection: [],
      turnStatus
    }
  },
  watch: {},
  created() {
    this.index()
  },
  methods: {
    search() {
      this.query.page = 1
      this.index()
    },
    selectionHandler(selection) {
      this.selection = selection
    },
    index() {
      this.loading = true
      API.managementList(this.query).then(response => {
        this.total = response.data.total
        this.managementList = response.data.data
      }).finally(() => {
        this.loading = false
      })
    },
    exportHandler() {
      const data = [
        [
          '编号',
          '名称',
          '资产',
          '规格型号',
          '使用部门',
          '日期',
          '制造厂商',
          '原值',
          '已提折旧',
          '净值',
          '状态',
          '原因',
          '使用部门意见',
          '归口管理部门意见',
          '资产管理部门意见'
        ]
      ]
      let status = ''
      if (this.selection.length === 0) {
        this.$message.error('请选择要到导出的数据')
        return false
      } else {
        this.selection.forEach((item) => {
          if (item.status === 0) {
            status = '测试'
          } else if (item.status === 1) {
            status = '启用'
          } else if (item.status === 2) {
            status = '临时移除'
          } else if (item.status === 3) {
            status = '停用'
          }
          data.push([
            item.coding,
            item.name,
            item.eq.name,
            item.eq.spec,
            item.eq.department.name,
            item.date,
            item.eq.manufacturer.name,
            item.old_value,
            item.depreciation,
            item.new_value,
            status,
            item.reason,
            item.shiyong,
            item.guikou,
            item.guanli
          ])
        })
      }
      const turn = {
        SheetNames: ['固定资产停用(启用)数据'],
        Sheets: {
          '固定资产停用(启用)数据': xlsx.utils.aoa_to_sheet(data)
        }
      }
      this.exportExec(turn, '固定资产停用(启用)报表.xlsx')
    },
    remove() {
      const selection = this.selection
      this.delMany(API, selection)
    },
    confirmDel(id) {
      this.delOne(API, id, false)
    },
    addHandler() {
      this.goUrl('/asset/management/management_add')
    },
    updateHandler(id) {
      const url = `/asset/management/management_edit/${id}`
      this.goUrl(url)
    },
    reloadHandler() {
      this.reload()
    },
    statusFormat(row) {
      return this.getStatusFormat(row)
    }
  }
}
</script>
